/**
 * jims
 */
package com.jims.master.modules.doctor.service;


import com.jims.master.common.data.StringData;
import com.jims.master.common.mapper.JsonMapper;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.doctor.dao.MedicalRecordDao;
import com.jims.master.modules.doctor.dao.PatsInTransferringDao;
import com.jims.master.modules.doctor.entity.PatVisit;
import com.jims.master.modules.doctor.entity.PatsInTransferring;
import com.jims.master.modules.doctor.vo.TurnInVo;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 医生流转病人病历service
 * @author CTQ
 * @version 2017-03-28 09:12:09
 */
@Service
@Transactional(readOnly = true)
public class MedicalRecordService extends CrudService<MedicalRecordDao,PatVisit>{
    @Autowired
    private PatsInTransferringDao patsInTransferringDao;
    /**
     * 查询所有待入当前医生本科室的患者列表
     * @param turnInVo
     * @return
     */
    public List<TurnInVo> waitDeptPatientList(TurnInVo turnInVo){
        return dao.waitDeptPatientList(turnInVo);
    }

    /**
     * 根据ID即pat_visit_index的ID/身份证号查询病人信息
     * @param turnInVo
     * @return
     */
    public TurnInVo findInfoByParam(TurnInVo turnInVo){
        List<TurnInVo> list = dao.waitDeptPatientList(turnInVo);
        if (list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }
    /**
     * 查询所有待转出病人列表
     * @param turnInVo
     * @return
     */
    public List<TurnInVo> findWaitTurnOutPatientList(TurnInVo turnInVo){
        return dao.findWaitTurnOutPatientList(turnInVo);
    }

    /**
     * 查询选中病人信息
     * @param turnInVo
     * @return
     */
    public TurnInVo findInfoByTurnOutParam(TurnInVo turnInVo){
        List<TurnInVo> list = dao.findWaitTurnOutPatientList(turnInVo);
        if (list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }



    /**
     * 查询所有待取消转出病人列表
     * @param turnInVo
     * @return
     */
    public List<TurnInVo> findWaitCancelPatientList(TurnInVo turnInVo){
        return dao.findWaitCancelPatientList(turnInVo);
    }

    /**
     *根据参数查询病人信息
     * @param turnInVo
     * @return
     */
    public TurnInVo findInfoByCancelParam(TurnInVo turnInVo){
        List<TurnInVo> list = dao.findWaitCancelPatientList(turnInVo);
        if (list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }


    /**
     * 新建病人病历
     * @param turnInVo
     * @return
     * @author CTQ
     */
    @Transactional(readOnly = false)
    public String turnInPatient(TurnInVo turnInVo) {
        //判断是新入还是转入
        PatsInTransferring pat = new PatsInTransferring();
        pat.setPatientId(turnInVo.getPatientId());
        pat.setVisitId(turnInVo.getPatVisitId());
        pat.setOrgId(UserUtils.getOrgId());
        List<PatsInTransferring> list = patsInTransferringDao.findList(pat);
        if(list!=null&&list.size()>0){
            PatsInTransferring pt = list.get(0);
            //根据转出科室是否为空判断是新入还是转入
            if (StringUtils.isNotBlank(pt.getDeptTransferOut())){
                turnInVo.setPatAction("D");
            }else {
                turnInVo.setPatAction("C");
            }
        }
        User user = UserUtils.getUser();
       String jsonInfo = JsonMapper.toJsonString(turnInVo);
        //登录人信息
        //{"id":"9bf6d4287ea243099689cf99aa06e16e","patVisitId":"6fb840d157554fa4bbb080925cb436b4","name":"阿莱克","idNo":"","patientId":"d9374dec1cee4450b3f78bddbdd46cc0","situationType":"9","deptStayed":"dc17ce201c6e4164a64ebbc4d3a74188","nursingClass":"0","doctorInCharge":"0dcf526d8883436db9e0b2dd2a16b8f8"}
//        {"id":"0dcf526d8883436db9e0b2dd2a16b8f8","isNewRecord":false,"createDate":"2017-03-30 14:59:48","updateDate":"2017-03-30 14:59:48","orgId":"4745915623bc439da8584b14857dae0b","userType":"3","currentLogin":"liuhe","orgDept":{"id":"dc17ce201c6e4164a64ebbc4d3a74188","isNewRecord":false,"sort":30,"parentId":"0"},"phone":"15855556666","idCard":"542527198407093263","name":"刘鹤","email":"erwq@45.com","persionId":"0f9010c049d74c10bbe8925934011443","title":"231","loginFlag":"1","orgSelfServiceList":[{"id":"d2d97efc06434a5799dfefa9049fbcf1","isNewRecord":false,"createDate":"2017-03-20 11:30:04","updateDate":"2017-03-20 11:30:08","orgId":"4745915623bc439da8584b14857dae0b","serviceName":"医生服务","sort":4}],"admin":false}
        String loginInfoJson = JsonMapper.toJsonString(user);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("jsonInfo",jsonInfo);
        map.put("loginInfo",loginInfoJson);
        map.put("result", "1123456");
        dao.turnInPatient(map);
        return (String)map.get("result") ;
    }

    /**
     * 取消新建病人病历
     * @param turnInVo
     * @return
     */
    @Transactional(readOnly = false)
    public StringData cancelTurnInPatient(TurnInVo turnInVo) {
        User user = UserUtils.getUser();
        String jsonInfo = JsonMapper.toJsonString(turnInVo);
        //登录人信息
        String loginInfoJson = JsonMapper.toJsonString(user);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("jsonInfo",jsonInfo);
        map.put("loginInfo",loginInfoJson);
        map.put("result_code", "1123456");
        map.put("result_data", "1123456");
        map = dao.cancelTurnInPatient(map);
        StringData sd = new StringData();
        sd.setCode(map.get("result_code")!=null?map.get("result_code").toString():"");
        sd.setData(map.get("result_data")!=null?map.get("result_data").toString():"");
        return sd;
    }

    /**
     * 医生移出病人病历
     * @param turnInVo
     * @return
     */
    @Transactional(readOnly = false)
    public StringData turnOutPatient(TurnInVo turnInVo) {
        User user = UserUtils.getUser();
        String jsonInfo = JsonMapper.toJsonString(turnInVo);
        //  {"id":"","patVisitId":"6fb840d157554fa4bbb080925cb436b4","name":"阿莱克","sex":"2","patientId":"d9374dec1cee4450b3f78bddbdd46cc0","deptStayed":"5568c1ae6ef249c69c5cb8424723f7f2","doctorInCharge":"0dcf526d8883436db9e0b2dd2a16b8f8","upperDoctor":"0dcf526d8883436db9e0b2dd2a16b8f8","directorDoctor":"0dcf526d8883436db9e0b2dd2a16b8f8"}
        // {"id":"0dcf526d8883436db9e0b2dd2a16b8f8","isNewRecord":false,"createDate":"2017-03-30 14:59:48","updateDate":"2017-03-30 14:59:48","orgId":"4745915623bc439da8584b14857dae0b","userType":"3","currentLogin":"liuhe","orgDept":{"id":"dc17ce201c6e4164a64ebbc4d3a74188","isNewRecord":false,"sort":30,"deptName":"骨科二","parentId":"0"},"phone":"15855556666","idCard":"542527198407093263","name":"刘鹤","email":"erwq@45.com","persionId":"0f9010c049d74c10bbe8925934011443","title":"231","loginFlag":"1","orgSelfServiceList":[{"id":"d2d97efc06434a5799dfefa9049fbcf1","isNewRecord":false,"createDate":"2017-03-20 11:30:04","updateDate":"2017-03-20 11:30:08","orgId":"4745915623bc439da8584b14857dae0b","serviceName":"医生服务","sort":4}],"currentService":{"id":"d2d97efc06434a5799dfefa9049fbcf1","isNewRecord":false,"createDate":"2017-03-20 11:30:04","updateDate":"2017-03-20 11:30:08","orgId":"4745915623bc439da8584b14857dae0b","serviceName":"医生服务","sort":4},"admin":false}

        //登录人信息
        String loginInfoJson = JsonMapper.toJsonString(user);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("jsonInfo",jsonInfo);
        map.put("loginInfo",loginInfoJson);
        map.put("result_code", "1123456");
        map.put("result_data", "1123456");
        map = dao.turnOutPatient(map);
        StringData sd = new StringData();
        sd.setCode(map.get("result_code")!=null?map.get("result_code").toString():"");
        sd.setData(map.get("result_data")!=null?map.get("result_data").toString():"");
        return sd ;
    }

    /**
     * 取消移出病人病历
     * @param turnInVo
     * @return
     */
    @Transactional(readOnly = false)
    public String cancleTurnOutPatient(TurnInVo turnInVo) {
        User user = UserUtils.getUser();
        String jsonInfo = JsonMapper.toJsonString(turnInVo);
        //登录人信息
        //{"id":"9bf6d4287ea243099689cf99aa06e16e","patVisitId":"6fb840d157554fa4bbb080925cb436b4","name":"阿莱克","sex":"2","patientId":"d9374dec1cee4450b3f78bddbdd46cc0","upperDoctor":"0dcf526d8883436db9e0b2dd2a16b8f8","directorDoctor":"0dcf526d8883436db9e0b2dd2a16b8f8"}
        //{"id":"0dcf526d8883436db9e0b2dd2a16b8f8","isNewRecord":false,"createDate":"2017-03-30 14:59:48","updateDate":"2017-03-30 14:59:48","orgId":"4745915623bc439da8584b14857dae0b","userType":"3","currentLogin":"liuhe","orgDept":{"id":"5568c1ae6ef249c69c5cb8424723f7f2","isNewRecord":false,"sort":30,"deptName":"骨科一","parentId":"0"},"phone":"15855556666","idCard":"542527198407093263","name":"刘鹤","email":"erwq@45.com","persionId":"0f9010c049d74c10bbe8925934011443","title":"231","loginFlag":"1","orgSelfServiceList":[{"id":"d2d97efc06434a5799dfefa9049fbcf1","isNewRecord":false,"createDate":"2017-03-20 11:30:04","updateDate":"2017-03-20 11:30:08","orgId":"4745915623bc439da8584b14857dae0b","serviceName":"医生服务","sort":4}],"currentService":{"id":"d2d97efc06434a5799dfefa9049fbcf1","isNewRecord":false,"createDate":"2017-03-20 11:30:04","updateDate":"2017-03-20 11:30:08","orgId":"4745915623bc439da8584b14857dae0b","serviceName":"医生服务","sort":4},"admin":false}
        String loginInfoJson = JsonMapper.toJsonString(user);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("jsonInfo",jsonInfo);
        map.put("loginInfo",loginInfoJson);
        map.put("result_code", "1123456");
        dao.cancelTurnOutPatient(map);
        return (String)map.get("result_code") ;
    }

}